package com.spring.framework.config.value;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author xuweizhi
 * @since 2020/12/15 14:45
 */
@Component
@SuppressWarnings("all")
@PropertySource(value = "classpath:config/value.properties", encoding = "UTF-8")
public class ValueProperties {

	//注入字面量，通过 ${键} 格式来获取值并注入到属性中

	@Value("${key}")
	private String value;
	@Value("${int}")
	private char c;
	@Value("${double}")
	private double d;
	@Value("${boolean}")
	private boolean b;
	@Value("${xxx}")
	private String xxx;

	@Value("${maohao}")
	private String maohao;
	@Value("${kongge}")
	private String kongge;

	//取多个属性
	@Value("${kongge}${maohao}")
	private String concat;


	//SPEL注入数组和单value集合的样式
	@Value("#{'${booleans}'.split(',')}")
	private boolean[] booleans;

	@Value("#{'${list}'.split(';')}")
	private List<String> stringList;

	@Value("#{'${set}'.split(',')}")
	private Set<String> stringSet;


	@Value("#{${properties}}")
	private Properties properties;

	@Value("#{${redirectUrl}}")
	private Map<String, String> map;


	//SPEL计算，当然还有其他更多复杂操作
	@Value("#{${int}+${double}}")
	private int add;

	@Value("#{${int}-${double}}")
	private int subtract;

	@Value("#{${int}*${double}}")
	private int multiply;

	@Value("#{${int}/${double}}")
	private int divide;


	@Override
	public String toString() {
		System.out.println("------properties-------");
		properties.forEach((o, o2) -> {
			System.out.println(o);
			System.out.println(o2);
		});
		System.out.println("------map-------");
		map.forEach((o, o2) -> {
			System.out.println(o);
			System.out.println(o2);
		});
		System.out.println("------str-------");
		return "ValueProperties{" +
				"value='" + value + '\'' +
				", c=" + c +
				", d=" + d +
				", b=" + b +
				", xxx='" + xxx + '\'' +
				", maohao='" + maohao + '\'' +
				", kongge='" + kongge + '\'' +
				", concat='" + concat + '\'' +
				", add='" + add + '\'' +
				", subtract='" + subtract + '\'' +
				", multiply='" + multiply + '\'' +
				", divide='" + divide + '\'' +
				", booleans=" + Arrays.toString(booleans) +
				", stringList=" + stringList +
				", stringSet=" + stringSet +
				", properties=" + properties +
				", map=" + map +
				'}';
	}

	public static void main(String[] args) {
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ValueProperties.class);
		System.out.println(applicationContext.getBean(ValueProperties.class));
	}
}
